package com.hxalex.simulationhandle.accessbilityService.receiver.wifi;

import android.content.Intent;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import com.hxalex.simulationhandle.accessbilityService.manger.WifiManger;
import com.hxalex.simulationhandle.accessbilityService.receiver.base.BaseReceiver;
import com.hxalex.simulationhandle.accessbilityService.receiver.wifi.service.WifiInfoService;

/**
 * Created by hx_Alex on 2018/5/27.
 */

public class CheckWifiIsConnectReceiver extends BaseReceiver {
    private AccessibilityNodeInfo info;
    private static final String TAG = "CheckWifiIsConnect";
    public CheckWifiIsConnectReceiver(WifiManger wifiManger) {
        super(wifiManger);
        FLAG="检查WIFI是否连接命令";
    }

    public AccessibilityNodeInfo getInfo() {
        return info;
    }

    public void setInfo(AccessibilityNodeInfo info) {
        this.info = info;
    }

    @Override
    public void doSomething() {
        if(!isPrintCommandMsg)
        {
            Log.w("CheckWifiIsConnect","开始执行"+FLAG);
            isPrintCommandMsg=true;
        }
        //wifi是否连接的检查
        Intent i = new Intent(WifiManger.getContext(), WifiInfoService.class);
        i.setAction(WifiInfoService.WIFI_PING);
        WifiManger.getContext().startService(i);
        commandCompleteListener.commandCompleteListener();
    }

    private boolean isHaveConnectWindow(AccessibilityNodeInfo info) {
         boolean reslut=false;
            if(info==null || info.getClassName()==null || info.getViewIdResourceName()==null) return reslut;
            if(info.getClassName().equals("android.widget.ListView") && info.getViewIdResourceName().equals("com.snda.wifilocating:id/dg_container"))
            {
                reslut=true;
               
            }else if(info.getClassName().equals("android.widget.TextView") && info.getViewIdResourceName().equals("com.snda.wifilocating:id/status"))
            {
                reslut=true;
            }
            {
                for (int i = 0; i < info.getChildCount(); i++) {
                    if (info.getChild(i) != null) {
                        isHaveConnectWindow(info.getChild(i));
                    }
                }
            }
           
      
        Log.i(TAG,"reslut:"+reslut);
        return reslut;
    }

    private boolean isHaveAlertWindow(AccessibilityNodeInfo info) {
        boolean reslut=false;
        if(info==null || info.getClassName()==null || info.getViewIdResourceName()==null) return reslut;
        if(info.getChildCount()==0)
        {
            if (info.getText()!=null && (info.getText().toString().equals("取消") || info.getText().toString().contains("我知道")) )
            {
                Log.i(TAG, "点击弹窗。。。。");
                info.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
        }
       else
        {
            for (int i = 0; i < info.getChildCount(); i++) {
                if (info.getChild(i) != null) {
                    isHaveAlertWindow(info.getChild(i));
                }
            }
        }


        Log.i(TAG,"reslut:"+reslut);
        return reslut;
    } 


 
}
